package com.yookey.config;

import com.yookey.common.Interceptor.RequestInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * className WebMvcConfig
 *
 * @Author: Zhang Shao Hui
 * @Description:
 * @Date:Create：in 2019/11/20 14:31
 * @Modified By：
 */
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private RequestInterceptor requestInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        String resourceLocation = "file:" + System.getProperty("user.dir") + "/avatar/";
        registry.addResourceHandler("/avatar/**").addResourceLocations(resourceLocation);

    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(requestInterceptor)
                // addPathPatterns 用于添加拦截规则 ， 先把所有路径都加入拦截， 再一个个排除
                .addPathPatterns("/**")
                // excludePathPatterns 表示改路径不用拦截
//                .excludePathPatterns("/user/login")
//                .excludePathPatterns("/user/register")
//                .excludePathPatterns("/user/verifyCode/send")
//                .excludePathPatterns("/file/upload")
//                .excludePathPatterns("/file/api/upload")
//                .excludePathPatterns("/home/detail")
//                .excludePathPatterns("/product/detail")
//                .excludePathPatterns("/product/find")
//                .excludePathPatterns("/catalog/find")
                .excludePathPatterns("/doc.html")
                .excludePathPatterns("/api-docs-ext")
                .excludePathPatterns("/v2/**")
                .excludePathPatterns("/api-docs")
                .excludePathPatterns("/swagger-ui.html")
                .excludePathPatterns("/configuration/ui")
                .excludePathPatterns("/swagger-resources/configuration/ui")
                .excludePathPatterns("/swagger-resources")
                .excludePathPatterns("/configuration/security")
                .excludePathPatterns("/v2/api-docs")
                .excludePathPatterns("/error")
                .excludePathPatterns("/webjars/**")
                .excludePathPatterns("/**/*.ico")
                .excludePathPatterns("/avatar/**")
                .excludePathPatterns("/login/**");
    }
}
